/**
 * 获取默认值
 * @param args
 */
export const getDefaultByNull = (...args) => {
  if (isNotNullArray(args)) {
    for (let i = 0; i < args.length; i++) {
      const val = args[i];
      if (val !== undefined && val !== null) {
        return val;
      }
    }
  }
};

/**
 * 判断是否不为空
 * @param val
 */
export const isNotNull = (val) => {
  return val !== undefined && val !== null;
};

/**
 * 判断是否为空数组
 * @param val
 */
export const isNotNullArray = (val) => {
  return val !== undefined && val !== null && isArray(val) && val.length > 0;
};

/**
 * 判断是否为对象类型
 * @param val
 */
export const isObject = (val) => {
  return typeof val === 'object';
};

/**
 * 判断是否为数组类型
 * @param val
 */
export const isArray = (val) => {
  return Object.prototype.toString.call(val) === '[object Array]';
};

/**
 * 判断是否为Date类型
 * @param val
 */
export const isDate = (val) => {
  // return val instanceof Date
  // return Object.prototype.toString.call(val) === '[object Date]';
  return val instanceof Date;
};

/**
 * 判断是否为true
 * @param val
 */
export const isTrue = (val) => {
  return val !== undefined && val !== null && val === true;
};

/**
 * 判断是否为false
 * @param val
 */
export const isFalse = (val) => {
  return val !== undefined && val !== null && val === false;
};

/**
 * 获取uuid
 */
export const getUuid = () => {
  return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = Math.random() * 16 | 0;
    const v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
};

/**
 * 解析属性名及其前缀
 * @param key
 */
export const resolvePrefixKey = (key: string): {
  prefix?: string, newKey: string
} | undefined => {
  if (key) {
    const result: {
      prefix?: string, newKey: string
    } = {
      prefix: undefined,
      newKey: key
    };
    const group = key.match(/[A-Z-_]/);
    if (group) {
      const index = group.index;
      if (index > 0) {
        const prefixIndex = index + 1;
        const prefix = key.substring(0, prefixIndex);
        let newKey = key.substring(prefixIndex);
        if (newKey) {
          newKey = newKey.replace(/^[-_]+/, '');
          if (newKey) {
            newKey = newKey.charAt(0).toLowerCase() + newKey.slice(1);
            result.prefix = prefix;
            result.newKey = newKey;
          }
        }
      }
    }
    return result;
  }
};

/**
 * 获取去掉前缀的首字母小写的属性名
 * @param str
 * @param prefix
 */
export const getFirstLowerStartPrefix = (str: string, prefix: string): string | undefined => {
  let result;
  let strStartIndex = str;
  if (str && prefix) {
    if (str.indexOf(prefix) === 0) {
      strStartIndex = str.substring(prefix.length);
    } else {
      return;
    }
  }
  if (strStartIndex) {
    if (strStartIndex.length > 1) {
      result = strStartIndex.charAt(0).toLowerCase() + strStartIndex.slice(1);
    } else {
      result = strStartIndex.toLowerCase();
    }
    if (result !== strStartIndex) {
      return result;
    }
  }
};